var express = require('express');
var Msdb = require("../../../db/Msdb.js");
var router = express.Router();   //可使用 express.Router 类创建模块化、可挂载的路由句柄
const jwt = require('jsonwebtoken');
var Excel = require('exceljs');
var multer = require('multer');
var UUID = require('uuid');
var request = require("request");
const { getxl_util, getnowdate, saveczlog_util, res_util, checkbysql, addformfield, getnowdate_djh, setformfield,
   insert_table, update_table_wstr, update_table_byid, resutdata_to_formdata, resutdata_to_formdata_arr, save_qlczslog_util,
   usequery } = require('./server_util.js')

const log = require('../../../logger.js');
const db = require('../../../db/Redisdb.js');
var logger = require('../../../logger.js').logger;

//查询供应商评价_当前正式合作供应商名单
router.post('/gyskpmb_lb', async function (req, res) {
   var rtdata = await usequery(`select gysdm, gysmc,'' as kpzqq,'' as kpzqz,'' as zl,'' as jf,'' as tscl,'' as fw,'' as yddf,'' as pddj from shipSRM_gys where hzzt='正式合作' order by gysdm asc`)

   res_util(res, rtdata, true, "查询成功")
})


//查询供应商档案变更列表
router.post('/gysbglb', async function (req, res) {
   var rtdata = await usequery(`select * from shipSRM_gys_bgjl order by gysmc,create_time desc`)

   res_util(res, rtdata, true, "查询成功")
})


//导入供应商档案 excel

router.post('/gys_import', async function (req, res) {
   var data = req.body
   var db = new Msdb()
   let errorRows = []
   let importSql = ``
   var gysdmData = await usequery(` select top 1 * from shipSRM_gys order by gysdm desc `)
   var nextgysdm = gysdmData.length > 0 ? parseInt(gysdmData[0].gysdm.slice(-5)) + 1 : '10000'
   for (var i = 0; i < data.length; i++) {
      // 检查供应商名称是否已存在
      let ifexist = await checkbysql(`select * from shipSRM_gys where gysmc='${data[i].gysmc}'`)
      let gysdm = 'GYS' + nextgysdm.toString().padStart(4, '0')
      if (!ifexist) {
         importSql += `insert into shipSRM_gys (id,create_time,create_by,gysmc,gysdm,ghcp,lxr,sj,gsdz,fkfs,dj,hzzt,erpdm)
      values(NEXT VALUE FOR SEQ_ID,getdate(),'admin','${data[i].gysmc}','${gysdm}','${data[i].ghcp}','${data[i].lxr}','${data[i].sj}',
      '${data[i].gsdz}','${data[i].fkfs}','${data[i].dj}','正式合作','${data[i].erpdm}')`
         nextgysdm = nextgysdm * 1 + 1
      }
      else {
         errorRows.push({ rowNumber: i + 2, gysmc: data[i].gysmc, reason: '供应商名称已存在' })
      }
      let ifryexist = await checkbysql(`select * from shipSRM_ryda where  zh='${gysdm}' and yhlx='租户用户'`)
      if (!ifryexist) {
         function generateRandomPassword() {
            let password = '';
            for (let i = 0; i < 6; i++) {
               password += Math.floor(Math.random() * 10);
            }
            return password;
         }
         importSql += `insert into shipSRM_ryda (id,create_time,create_by,xm,js,jsid,yhlx,dh,zh,mm,ryzt,glgys)
            values(NEXT VALUE FOR SEQ_ID,getdate(),'admin','${data[i].gysmc}','供应商','1002226','租户用户','${data[i].sj}','${gysdm}','${generateRandomPassword()}','正常','${data[i].gysmc}')`
      }
   }
   await db.update(importSql)
   res_util(res, { errorRows }, true, "导入完成")
})

//新增供应商评价
router.post('/add_gyskp', async function (req, res) {
   let { formdata, storage } = req.body
   var insrtobj = await insert_table(formdata, "shipSRM_gyskp")
   await saveczlog_util(insrtobj.insertsql, storage, '', '新增供应商评价', '新增', 'shipSRM_gyskp')
   res_util(res, { formdata }, true, "新增成功")
})

//导入供应商考评 excel

router.post('/gyskp_import', async function (req, res) {
   var {data} = req.body
   var db = new Msdb()
   let errorRows = []
   let importSql = ``
   console.log("当前传递的data",data);
   for (var i = 0; i < data.length; i++) {
      let ifexist = await checkbysql(`select * from shipSRM_gyskp where gysmc='${data[i].gysmc}' and kpzqq='${data[i].kpzqq}' and kpzqz='${data[i].kpzqz}'`)
      if (!ifexist) {
         importSql += `insert into shipSRM_gyskp (id,create_time,create_by,gysmc,kpzqq,kpzqz,pchgl,jyhgps,zjlps,zcyccs,tscs,zldf,asjhl,asjhps,zjlps_jf,jfdf,
         zjcs,jjcs,jgdf,ycfk,clsx,fw,fwdf,zdf)
    values(NEXT VALUE FOR SEQ_ID,getdate(),'admin','${data[i].gysmc}','${data[i].kpzqq}','${data[i].kpzqz}','${data[i].pchgl?data[i].pchgl:0}',
    '${data[i].jyhgps?data[i].jyhgps:0}','${data[i].zjlps?data[i].zjlps:0}','${data[i].zcyccs?data[i].zcyccs:0}','${data[i].tscs?data[i].tscs:0}','${data[i].zldf?data[i].zldf:0}',
    '${data[i].asjhl?data[i].asjhl:0}','${data[i].asjhps?data[i].asjhps:0}','${data[i].zjlps_jf?data[i].zjlps_jf:0}',
    '${data[i].jfdf?data[i].jfdf:0}','${data[i].zjcs?data[i].zjcs:0}','${data[i].jjcs?data[i].jjcs:0}','${data[i].jgdf?data[i].jgdf:0}',
    '${data[i].ycfk?data[i].ycfk:0}','${data[i].clsx?data[i].clsx:0}','${data[i].fw?data[i].fw:0}','${data[i].fwdf?data[i].fwdf:0}',
    '${data[i].zdf?data[i].zdf:0}')`
      }
      else {
         errorRows.push({ rowNumber: i + 2, gysmc: data[i].gysmc,kpzqq:data[i].kpzqq,kpzqz:data[i].kpzqz })
      }
   }
   await db.update(importSql)
   let rtdata = await usequery(`update shipSRM_gyskp set gysdm=a.gysdm from shipSRM_gys a,shipSRM_gyskp b 
      where a.gysmc=b.gysmc `)

   res_util(res, { errorRows }, true, "导入完成")
})


//检查供应商名称是否存在
router.post('/check_gysmc_exist', async function (req, res) {
   let { gysmc } = req.body
   var ifExist = await checkbysql(`select * from shipSRM_gys where gysmc='${gysmc}'`)
   res_util(res, { exist: ifExist ? true : false }, true, "查询成功")
})

//查询供应商档案列表
router.post('/gyslb', async function (req, res) {
   var rtdata = await usequery(`select * from shipSRM_gys order by gysdm desc`)
   res_util(res, { rtdata }, true, "查询成功")
})

//查询供应商档案列表通过id
router.post('/query_gys_byid', async function (req, res) {
   let data = req.body.data
   var rtdata = await usequery(`select * from shipSRM_gys where id='${data.id}'`)
   var ghmxdata = await usequery(`select b.* from shipSRM_gys a,shipSRM_gys_ghmx b where a.id=b.mainid and a.id='${data.id}'`)
   // var sqmxdata = await usequery(`select b.* from shipSRM_gys a,shipSRM_gys_sqmx b where a.id=b.mainid and a.id='${data.id}'`)
   // var yhmxdata = await usequery(`select b.* from shipSRM_gys a,shipSRM_gys_yhmx b where a.id=b.mainid and a.id='${data.id}'`)
   var zzmxdata = await usequery(`select b.* from shipSRM_gys a,shipSRM_gys_zsmx b where a.id=b.mainid and a.id='${data.id}'`)
   var kpmxdata = await usequery(`select * from shipSRM_gyskp where gysmc='${data.gysmc}'`)

   res_util(res, { formdata: rtdata[0], ghmxdata, zzmxdata, kpmxdata }, true, "查询成功")
})

//查询供应商档案列表通过bm
router.post('/query_gys_bydm', async function (req, res) {
   var data = await usequery(`select * from shipSRM_gys where gysdm='${req.body.gysdm}'`)
   var ghmxdata = await usequery(`select b.* from shipSRM_gys a,shipSRM_gys_ghmx b where a.id=b.mainid and a.gysdm='${req.body.gysdm}'`)
   // var sqmxdata = await usequery(`select b.* from shipSRM_gys a,shipSRM_gys_sqmx b where a.id=b.mainid and a.gysdm='${req.body.gysdm}'`)
   // var yhmxdata = await usequery(`select b.* from shipSRM_gys a,shipSRM_gys_yhmx b where a.id=b.mainid and a.gysdm='${req.body.gysdm}'`)
   var zzmxdata = await usequery(`select b.* from shipSRM_gys a,shipSRM_gys_zsmx b where a.id=b.mainid and a.gysdm='${req.body.gysdm}'`)
   var kpmxdata = await usequery(`select b.* from shipSRM_gys a,shipSRM_gys_kpmx b where a.id=b.mainid and a.gysdm='${req.body.gysdm}'`)
   res_util(res, { formdata: data[0], ghmxdata, zzmxdata, kpmxdata }, true, "查询成功")
})

//新增供应商档案
router.post('/add_gys', async function (req, res) {
   let { formdata, ghmxdata, sqmxdata, yhmxdata, zzmxdata, kpmxdata, storage } = req.body
   let db = new Msdb()
   
   // 检查供应商名称是否重复
   var ifGysmcExist = await checkbysql(`select * from shipSRM_gys where gysmc='${formdata.gysmc.value}'`)
   if (ifGysmcExist) {
      res_util(res, { formdata }, false, "新增失败，供应商名称已存在")
      return
   }
   
   console.log("当前传递的storage", storage.yhlx)
   var gysdmData = await usequery(` select top 1 * from shipSRM_gys order by gysdm desc `)
   var nextgysdm = gysdmData.length > 0 ? parseInt(gysdmData[0].gysdm.slice(-5)) + 1 : '10000'
   formdata.gysdm.value = 'GYS' + nextgysdm.toString().padStart(4, '0')
   //不存在编码插入

   var insrtobj = await insert_table(formdata, "shipSRM_gys")
   await saveczlog_util(insrtobj.insertsql, storage, '', '新增供应商档案', '新增', 'shipSRM_gys')
   formdata = insrtobj.data;
   var mainid = insrtobj.data.id.value;
   for (var mxkey in ghmxdata) {
      let mxitem = ghmxdata[mxkey];
      mxitem.mainid = { dataIndex: "mainid", value: mainid, ifdb: true }
      var rtmxobj = await insert_table(mxitem, "shipSRM_gys_ghmx")
      await saveczlog_util(rtmxobj.insertsql, storage, '', '新增供应商供货明细', '新增', 'shipSRM_gys_ghmx')
   }
   // for (var mxkey in sqmxdata) {
   //    let mxitem = sqmxdata[mxkey];
   //    mxitem.mainid = { dataIndex: "mainid", value: mainid, ifdb: true }
   //    var rtmxobj = await insert_table(mxitem, "shipSRM_gys_sqmx")
   //    await saveczlog_util(rtmxobj.insertsql, storage, '', '新增供应商授权明细', '新增', 'shipSRM_gys_sqmx')                     
   // }
   // for (var mxkey in yhmxdata) {
   //    let mxitem = yhmxdata[mxkey];
   //    mxitem.mainid = { dataIndex: "mainid", value: mainid, ifdb: true }
   //    var rtmxobj = await insert_table(mxitem, "shipSRM_gys_yhmx")
   //    await saveczlog_util(rtmxobj.insertsql, storage, '', '新增供应商银行明细', '新增', 'shipSRM_gys_yhmx')                     
   // }
   for (var mxkey in zzmxdata) {
      let mxitem = zzmxdata[mxkey];
      mxitem.mainid = { dataIndex: "mainid", value: mainid, ifdb: true }
      var rtmxobj = await insert_table(mxitem, "shipSRM_gys_zsmx")
      await saveczlog_util(rtmxobj.insertsql, storage, '', '新增供应商资质明细', '新增', 'shipSRM_gys_zsmx')
   }
   for (var mxkey in kpmxdata) {
      let mxitem = kpmxdata[mxkey];
      mxitem.mainid = { dataIndex: "mainid", value: mainid, ifdb: true }
      var rtmxobj = await insert_table(mxitem, "shipSRM_gys_kpmx")
      await saveczlog_util(rtmxobj.insertsql, storage, '', '新增供应商考评明细', '新增', 'shipSRM_gys_kpmx')
   }

   var rtghmxdata = await usequery(`select *  from shipSRM_gys_ghmx where mainid ='${formdata.id.value}'`)
   // var rtsqmxdata = await usequery(`select *  from shipSRM_gys_sqmx where mainid ='${formdata.id.value}'`)
   // var rtyhmxdata = await usequery(`select *  from shipSRM_gys_yhmx where mainid ='${formdata.id.value}'`)
   var rtzzmxdata = await usequery(`select *  from shipSRM_gys_zsmx where mainid ='${formdata.id.value}'`)
   var rtkpmxdata = await usequery(`select *  from shipSRM_gys_kpmx where mainid ='${formdata.id.value}'`)

   let ifryexist = await checkbysql(`select * from shipSRM_ryda where yhlx='租户用户' and zh='${formdata.gysdm.value}'`)
   if (!ifryexist) {
      function generateRandomPassword() {
         let password = '';
         for (let i = 0; i < 6; i++) {
            password += Math.floor(Math.random() * 10);
         }
         return password;
      }
      let ludpsql = `insert into shipSRM_ryda (id,create_time,create_by,xm,js,jsid,yhlx,dh,zh,mm,ryzt,glgys)
         values(NEXT VALUE FOR SEQ_ID,getdate(),'admin','${formdata.gysmc.value}','供应商','1002226','租户用户','${formdata.sj.value}','${formdata.gysdm.value}',
         '${generateRandomPassword()}','正常','${formdata.gysmc.value}')`
      await db.update(ludpsql)
   }

   res_util(res, { formdata: formdata, ghmxdata: rtghmxdata, zzmxdata: rtzzmxdata, kpmxdata: rtkpmxdata }, true, "新增成功")
})




//新增供应商档案
router.post('/add_gysbg', async function (req, res) {
   let { formdata, ghmxdata, sqmxdata, yhmxdata, zzmxdata, storage } = req.body
   // var ifwlbm = await checkbysql(` select top 1 * from shipSRM_gys where gysdm='${formdata.gysdm.value}'`)
   // if (ifwlbm) {
   //    res_util(res, { formdata }, false, "新增失败，供应商编码已存在")
   //    return
   // }
   // 名称重复检查（暂存也不允许重名，排除当前供应商自身）
   var ifGysmcExistDraft = await checkbysql(`select * from shipSRM_gys where gysmc='${formdata.gysmc.value}'`)
   if (ifGysmcExistDraft) {
      res_util(res, { formdata }, false, "暂存失败，供应商名称已存在")
      return
   }
   formdata.clzt.value = "未审批"
   formdata.bgsj.value = getnowdate()
   let dqid = formdata.id.value;
   var insrtobj = await insert_table(formdata, "shipSRM_gys_bgjl")
   let db = new Msdb()
   await db.update(`update shipSRM_gys_bgjl set bgsj = getdate() where id = '${dqid}'`)
   await saveczlog_util(insrtobj.insertsql, storage, '', '新增供应商档案变更记录', '新增', 'shipSRM_gys_bgjl')
   formdata = insrtobj.data;
   var mainid = insrtobj.data.id.value;
   for (var mxkey in ghmxdata) {
      let mxitem = ghmxdata[mxkey];
      mxitem.mainid = { dataIndex: "mainid", value: mainid, ifdb: true }
      var rtmxobj = await insert_table(mxitem, "shipSRM_gysbg_ghmx")
      await saveczlog_util(rtmxobj.insertsql, storage, '', '新增供应商供货明细', '新增', 'shipSRM_gysbg_ghmx')
   }
   for (var mxkey in sqmxdata) {
      let mxitem = sqmxdata[mxkey];
      mxitem.mainid = { dataIndex: "mainid", value: mainid, ifdb: true }
      var rtmxobj = await insert_table(mxitem, "shipSRM_gysbg_sqmx")
      await saveczlog_util(rtmxobj.insertsql, storage, '', '新增供应商授权明细', '新增', 'shipSRM_gysbg_sqmx')
   }
   for (var mxkey in yhmxdata) {
      let mxitem = yhmxdata[mxkey];
      mxitem.mainid = { dataIndex: "mainid", value: mainid, ifdb: true }
      var rtmxobj = await insert_table(mxitem, "shipSRM_gysbg_yhmx")
      await saveczlog_util(rtmxobj.insertsql, storage, '', '新增供应商银行明细', '新增', 'shipSRM_gysbg_yhmx')
   }
   for (var mxkey in zzmxdata) {
      let mxitem = zzmxdata[mxkey];
      mxitem.mainid = { dataIndex: "mainid", value: mainid, ifdb: true }
      var rtmxobj = await insert_table(mxitem, "shipSRM_gysbg_zsmx")
      await saveczlog_util(rtmxobj.insertsql, storage, '', '新增供应商资质明细', '新增', 'shipSRM_gysbg_zsmx')
   }

   var rtghmxdata = await usequery(`select *  from shipSRM_gys_ghmx where mainid ='${formdata.id.value}'`)
   var rtsqmxdata = await usequery(`select *  from shipSRM_gys_sqmx where mainid ='${formdata.id.value}'`)
   var rtyhmxdata = await usequery(`select *  from shipSRM_gys_yhmx where mainid ='${formdata.id.value}'`)
   var rtzzmxdata = await usequery(`select *  from shipSRM_gys_zsmx where mainid ='${formdata.id.value}'`)


   res_util(res, { formdata: formdata, ghmxdata: rtghmxdata, sqmxdata: rtsqmxdata, yhmxdata: rtyhmxdata, zzmxdata: rtzzmxdata }, true, "新增成功")
})

//通过列表查看变更记录
router.post('/query_gysbg_byid', async function (req, res) {
   let data = req.body.data
   var rtdata = await usequery(`select * from shipSRM_gys_bgjl where id='${data}'`)
   var ghmxdata = await usequery(`select b.* from shipSRM_gys_bgjl a,shipSRM_gysbg_ghmx b where a.id=b.mainid and a.id='${data}'`)
   var sqmxdata = await usequery(`select b.* from shipSRM_gys_bgjl a,shipSRM_gysbg_sqmx b where a.id=b.mainid and a.id='${data}'`)
   var yhmxdata = await usequery(`select b.* from shipSRM_gys_bgjl a,shipSRM_gysbg_yhmx b where a.id=b.mainid and a.id='${data}'`)
   var zzmxdata = await usequery(`select b.* from shipSRM_gys_bgjl a,shipSRM_gysbg_zsmx b where a.id=b.mainid and a.id='${data}'`)


   res_util(res, { formdata: rtdata[0], ghmxdata, sqmxdata, yhmxdata, zzmxdata }, true, "查询成功")
})

//拉黑
router.post('/lhei', async function (req, res) {
   let { formdata, blacklistReason, storage } = req.body
   formdata.lhyy.value = blacklistReason
   formdata.hzzt.value = "黑名单"
   formdata.lhrq.value = getnowdate()
   let updatobj = await update_table_byid(formdata, "shipSRM_gys")
   await saveczlog_util(updatobj.updatesql, storage, '', '更新供应商档案', '更新', 'shipSRM_gys')
   res_util(res, { formdata }, true, "拉黑成功")
})

//准入
router.post('/zhunru', async function (req, res) {
   let { formdata, storage } = req.body
   formdata.hzzt.value = "备选"
   let updatobj = await update_table_byid(formdata, "shipSRM_gys")
   await saveczlog_util(updatobj.updatesql, storage, '', '更新供应商档案', '更新', 'shipSRM_gys')
   console.log("当前传递的formdata", formdata)
   res_util(res, { formdata }, true, "供应商准入成功")
})

//应用供应商档案变更
router.post('/apply_gysbg', async function (req, res) {
   let { formdata, ghmxdata, sqmxdata, yhmxdata, zzmxdata, storage } = req.body
   let db = new Msdb()
   var rtdata = await usequery(`select * from shipSRM_gys where gysdm='${formdata.gysdm.value}'`)
   // 更新供应商变更档案
   formdata.clzt.value = '已通过'
   let updatobj = await update_table_byid(formdata, "shipSRM_gys_bgjl")
   await saveczlog_util(updatobj.updatesql, storage, '', '应用供应商档案变更', '更新', 'shipSRM_gysbg')

   let newfromdata = formdata
   newfromdata.id.value = rtdata[0].id
   delete newfromdata.clzt
   delete newfromdata.bgxj
   delete newfromdata.bgsj
   let updatobj2 = await update_table_byid(newfromdata, "shipSRM_gys")
   await saveczlog_util(updatobj2.updatesql, storage, '', '供应商档案更新', '更新', 'shipSRM_gys')

   //删除旧的供应商供货明细
   await db.update(`delete from shipSRM_gys_ghmx where mainid= ${rtdata[0].id}`)
   for (var mxkey in ghmxdata) {
      let mxitem = ghmxdata[mxkey];
      mxitem.mainid = { dataIndex: "mainid", value: rtdata[0].id, ifdb: true }
      var rtmxobj = await insert_table(mxitem, "shipSRM_gys_ghmx")
      await saveczlog_util(rtmxobj.insertsql, storage, '', '新增供应商供货明细', '新增', 'shipSRM_gys_ghmx')
   }

   //删除旧的供应商授权明细
   await db.update(`delete from shipSRM_gys_sqmx where mainid= ${rtdata[0].id}`)
   for (var mxkey in sqmxdata) {
      let mxitem = sqmxdata[mxkey];
      mxitem.mainid = { dataIndex: "mainid", value: rtdata[0].id, ifdb: true }
      var rtmxobj = await insert_table(mxitem, "shipSRM_gys_sqmx")
      await saveczlog_util(rtmxobj.insertsql, storage, '', '新增供应商授权明细', '新增', 'shipSRM_gys_sqmx')
   }

   //删除旧的供应商银行明细  
   await db.update(`delete from shipSRM_gys_yhmx where mainid= ${rtdata[0].id}`)
   for (var mxkey in yhmxdata) {
      let mxitem = yhmxdata[mxkey];
      mxitem.mainid = { dataIndex: "mainid", value: rtdata[0].id, ifdb: true }
      var rtmxobj = await insert_table(mxitem, "shipSRM_gys_yhmx")
      await saveczlog_util(rtmxobj.insertsql, storage, '', '新增供应商银行明细', '新增', 'shipSRM_gys_yhmx')
   }

   //删除旧的供应商资质明细
   await db.update(`delete from shipSRM_gys_zsmx where mainid= ${rtdata[0].id}`)
   for (var mxkey in zzmxdata) {
      let mxitem = zzmxdata[mxkey];
      mxitem.mainid = { dataIndex: "mainid", value: rtdata[0].id, ifdb: true }
      var rtmxobj = await insert_table(mxitem, "shipSRM_gys_zsmx")
      await saveczlog_util(rtmxobj.insertsql, storage, '', '新增供应商资质明细', '新增', 'shipSRM_gys_zsmx')
   }


   res_util(res, { formdata: updatobj.data }, true, "审核通过成功")
})

//应用供应商档案变更
router.post('/refuse_gysbg', async function (req, res) {
   let { formdata, storage } = req.body
   let db = new Msdb()
   var rtdata = await usequery(`select * from shipSRM_gys where gysdm='${formdata.gysdm.value}'`)
   // 更新供应商变更档案
   formdata.clzt.value = '已驳回'
   let updatobj = await update_table_byid(formdata, "shipSRM_gys_bgjl")
   await saveczlog_util(updatobj.updatesql, storage, '', '驳回供应商档案变更', '更新', 'shipSRM_gysbg')
   res_util(res, { formdata: updatobj.data }, true, "驳回成功")
})


//更新供应商档案
router.post('/update_gys', async function (req, res) {
   let { formdata, ghmxdata, sqmxdata, yhmxdata, zzmxdata, ghdelidarr, sqdelidarr, yhdelidarr, zzdelidarr, storage } = req.body
   
   // 检查供应商名称是否重复（排除当前供应商自己）
   var ifGysmcExist = await checkbysql(`select * from shipSRM_gys where gysmc='${formdata.gysmc.value}' `)
   if (ifGysmcExist) {
      res_util(res, { formdata }, false, "更新失败，供应商名称已存在")
      return
   }
   
   //不存在更新
   let db = new Msdb()
   let updatobj = await update_table_byid(formdata, "shipSRM_gys")
   await saveczlog_util(updatobj.updatesql, storage, '', '更新供应商档案', '更新', 'shipSRM_gys')

   var mainid = updatobj.data.id.value;
   for (var mxkey in ghmxdata) {
      let mxitem = ghmxdata[mxkey];
      var ifcz = await checkbysql(`select * from shipSRM_gys_ghmx where id='${mxitem.id.value}'`)
      mxitem.mainid = { dataIndex: "mainid", value: mainid, ifdb: true }
      var rtmxobj = {}
      if (!ifcz) {
         rtmxobj = await insert_table(mxitem, "shipSRM_gys_ghmx")
      } else {
         rtmxobj = await update_table_byid(mxitem, "shipSRM_gys_ghmx")
      }
   }
   // for(var mxkey in sqmxdata){
   //    let mxitem = sqmxdata[mxkey];
   //    var ifcz = await checkbysql(`select * from shipSRM_gys_sqmx where id='${mxitem.id.value}'`)
   //    mxitem.mainid = { dataIndex: "mainid", value: mainid, ifdb: true }
   //    var rtmxobj = {}
   //    if (!ifcz) {
   //       rtmxobj = await insert_table(mxitem, "shipSRM_gys_sqmx")
   //    } else {
   //       rtmxobj = await update_table_byid(mxitem, "shipSRM_gys_sqmx")
   //    }
   // }
   // for(var mxkey in yhmxdata){
   //    let mxitem = yhmxdata[mxkey];
   //    var ifcz = await checkbysql(`select * from shipSRM_gys_yhmx where id='${mxitem.id.value}'`)
   //    mxitem.mainid = { dataIndex: "mainid", value: mainid, ifdb: true }
   //    var rtmxobj = {}
   //    if (!ifcz) {
   //       rtmxobj = await insert_table(mxitem, "shipSRM_gys_yhmx")
   //    } else {
   //       rtmxobj = await update_table_byid(mxitem, "shipSRM_gys_yhmx")
   //    }
   // }
   for (var mxkey in zzmxdata) {
      let mxitem = zzmxdata[mxkey];
      var ifcz = await checkbysql(`select * from shipSRM_gys_zsmx where id='${mxitem.id.value}'`)
      mxitem.mainid = { dataIndex: "mainid", value: mainid, ifdb: true }
      var rtmxobj = {}
      if (!ifcz) {
         rtmxobj = await insert_table(mxitem, "shipSRM_gys_zsmx")
      } else {
         rtmxobj = await update_table_byid(mxitem, "shipSRM_gys_zsmx")
      }
   }

   //删除delidarr
   await db.update(`delete from shipSRM_gys_ghmx where id in (${ghdelidarr.map(item => `'${item}'`).join(',')}) `)
   // await db.update(`delete from shipSRM_gys_sqmx where id in (${sqdelidarr.map(item => `'${item}'`).join(',')}) `)
   // await db.update(`delete from shipSRM_gys_yhmx where id in (${yhdelidarr.map(item => `'${item}'`).join(',')}) `)
   await db.update(`delete from shipSRM_gys_zsmx where id in (${zzdelidarr.map(item => `'${item}'`).join(',')}) `)

   var rtghmxdata = await usequery(`select *  from shipSRM_gys_ghmx where mainid ='${formdata.id.value}'`)
   // var rtsqmxdata = await usequery(`select *  from shipSRM_gys_sqmx where mainid ='${formdata.id.value}'`)
   // var rtyhmxdata = await usequery(`select *  from shipSRM_gys_yhmx where mainid ='${formdata.id.value}'`)
   var rtzzmxdata = await usequery(`select *  from shipSRM_gys_zsmx where mainid ='${formdata.id.value}'`)
   var rtkpmxdata = await usequery(`select *  from shipSRM_gys_kpmx where mainid ='${formdata.id.value}'`)


   res_util(res, { formdata: updatobj.data, ghmxdata: rtghmxdata, zzmxdata: rtzzmxdata, kpmxdata: rtkpmxdata }, true, "更新成功")
})

//删除供应商档案
router.post('/delete_gys', async function (req, res) {
   let { selectedRows, storage } = req.body
   var db = new Msdb()
   const delsql = `delete from shipSRM_gys where id in (${selectedRows.map(item => `'${item.id}'`).join(',')})`
   await db.update(delsql)
   await saveczlog_util(delsql, storage, '', '删除供应商档案', '删除', 'shipSRM_wlda')
   res_util(res, {}, true, "删除成功")
})

//查询供应商评价列表
router.post('/gyskplb', async function (req, res) {
   var rtdata = await usequery(`select * from shipSRM_gyskp order by kpzqq,gysdm desc`)
   res_util(res, rtdata, true, "查询成功")
})

//查询供应商评价列表通过id
router.post('/query_gyskp_byid', async function (req, res) {
   var data = await usequery(`select * from shipSRM_gyskp where id='${req.body.id}' order by create_time desc`)
   res_util(res, { formdata: data[0] }, true, "查询成功")
})

// //新增供应商评价
// router.post('/add_gyskp', async function (req, res) {
//    let { formdata, storage } = req.body
//    var ifwlbm = await checkbysql(` select top 1 * from shipSRM_gyskp where gh='${formdata.gh.value}'`)
//    if (ifwlbm) {
//       res_util(res, { formdata }, false, "新增失败，工号已存在")
//       return
//    }
//    //不存在编码插入
//    var insrtobj = await insert_table(formdata, "shipSRM_gyskp")
//    await saveczlog_util(insrtobj.insertsql, storage, '', '新增供应商评价', '新增', 'shipSRM_gyskp')
//    res_util(res, { formdata: insrtobj.data }, true, "新增成功")
// })

// //更新供应商评价
// router.post('/update_gyskp', async function (req, res) {
//    let { formdata, storage } = req.body
//    var ifwlbm = await checkbysql(` select top 1 * from shipSRM_gyskp where 
//    zh='${formdata.zh.value}' and id !='${formdata.id.value}'`)
//    if (ifwlbm) {
//       res_util(res, { formdata }, false, "更新失败，账号已存在")
//       return
//    }
//    //不存在更新
//    let updatobj = await update_table_byid(formdata, "shipSRM_gyskp")
//    await saveczlog_util(updatobj.updatesql, storage, '', '更新供应商评价', '更新', 'shipSRM_gyskp')
//    res_util(res, { formdata: updatobj.data }, true, "更新成功")
// })


// //删除供应商评价
// router.post('/delete_gyskp', async function (req, res) {
//    let { selectedRows, storage } = req.body
//    var db=new Msdb()
//    const delsql = `delete from shipSRM_gyskp where id in (${selectedRows.map(item => `'${item.id}'`).join(',')})`
//    await db.update(delsql)
//    await saveczlog_util(delsql, storage, '', '删除供应商评价', '删除', 'shipSRM_gyskp')
//    res_util(res, {}, true, "删除成功")
// })

module.exports = router; 